﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Configuration;
using System.Collections.Concurrent;

namespace YLPublicService
{
    public abstract class ServiceBase : IServiceBase
    {
        protected static DbDataContext dbContext;
        protected static ConcurrentDictionary<string, TransferModel.User> onlineUserByLoginName;
        protected static ConcurrentDictionary<Guid, TransferModel.User> onlineUserByGuid;

        protected static ConcurrentDictionary<string, Guid> currentUserGuids;
        protected static ConcurrentDictionary<Guid, dynamic> _currentUsers;
        /// <summary>
        /// ServerUser 或者User
        /// </summary>
        protected dynamic currentUser(Guid guid)
        {
            return _currentUsers[guid];
        }
        /// <summary>
        /// 传入为null是 将currentUser设置成null
        /// </summary>
        /// <param name="user"></param>
        protected abstract void setCurrentUser(TransferModel.User user, Guid guid);
        //protected TransferModel.User currentUser { get; set; }

        static ServiceBase()
        {
            dbContext = new DbDataContext();
            onlineUserByLoginName = new ConcurrentDictionary<string, TransferModel.User>();
            onlineUserByGuid = new ConcurrentDictionary<Guid, TransferModel.User>();
            currentUserGuids = new ConcurrentDictionary<string, Guid>();
            _currentUsers = new ConcurrentDictionary<Guid, dynamic>();
        }
        /// <summary>
        /// 登录成功返回当前guid,否则返回空字符串
        /// 如果已经登录返回之前的guid
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Guid Login(string loginName, string pwd)
        {
            TransferModel.User user = new TransferModel.User();
            user.Initial(loginName, pwd);

            if (dbContainsUser(loginName, pwd))
            {
                if (!isOnline(user.LoginName))
                {
                    user.Enabled = true;
                    onlineUserByLoginName[user.LoginName] = user;
                    onlineUserByGuid[user.Guid] = user;
                    setCurrentUser(user, user.Guid);
                    currentUserGuids[loginName] = user.Guid;
                    return user.Guid;
                }
                else
                {

                    return currentUserGuids[loginName];
                }
            }
            else
            {
                return Guid.Empty;
            }
        }

        /// <summary>
        /// 注销用户
        /// </summary>
        /// <param name="guid"></param>
        public void LoginOut(Guid guid)
        {
            if (!isOnline(guid))
                return;
            dynamic user = currentUser(guid);
            TransferModel.User tu = new TransferModel.User();
            while (onlineUserByLoginName.TryRemove(user.LoginName, out tu) == false) ;
            while (onlineUserByGuid.TryRemove(guid, out tu) == false) ;
            setCurrentUser(null, guid);
        }

        /// <summary>
        /// 数据库中是否存在该用户（普通或者管理员，根据子类各自实现）
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        protected abstract bool dbContainsUser(string loginName, string pwd);
        protected bool isOnline(string loginName)
        {
            return onlineUserByLoginName.ContainsKey(loginName);
        }
        /// <summary>
        /// 除了登录以外都要使用判断
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        protected bool isOnline(Guid guid)
        {
            return onlineUserByGuid.ContainsKey(guid);
        }
    }
}